home *** CD-ROM | disk | FTP | other *** search
/ The Programmer Disk / The Programmer Disk (Microforum).iso / xpro / c2 / pro15 / dbobj.h < prev    next >
C/C++ Source or Header  |  1992-10-13  |  5KB  |  157 lines

  1. // DBOBJ.H
  2. //
  3. // Definitions for use by DBObject and client programs
  4. //
  5. // (c) 1992 ShaunB - CompuServe: 70043,2641
  6. //
  7. // Placed in the public domain by the author
  8. //
  9. // Permission is hereby given to make modifications to any code provided.
  10. //
  11. // Author accepts no responsibility for breaking your files !!
  12.  
  13.  
  14. #ifndef _DBOBJ_H
  15. #define _DBOBJ_H
  16.  
  17. // Some errors we might encounter
  18. #define ER_UNDEFRECORD            1
  19. #define ER_BADRECREAD            2
  20. #define ER_BADRECWRITE            3
  21. #define ER_BADHDRREAD            4
  22. #define ER_BADHDRWRITE            5
  23. #define ER_BADOPEN                6
  24. #define ER_BADCLOSE                7
  25. #define ER_BADRECNUM                8
  26. #define ER_NOEXIST                9
  27.  
  28. // Define a few datatypes
  29. typedef unsigned char    byte;
  30. typedef unsigned int      word;
  31. typedef unsigned long     dword;
  32. typedef unsigned int        bool;
  33.  
  34. const int true  = 1;
  35. const int false = 0;
  36. const int NoError = 0;
  37.  
  38. // Structure of a dBase III+ field descriptor
  39. struct DBField {
  40.     char    fldName[11];
  41.     char    fldType;
  42.     dword    dataAddress;
  43.     byte    fldLen;
  44.     byte    fldDec;
  45.     byte    reserved[14];
  46. };
  47.  
  48. // Structure of a dBase III+ header
  49. struct DBHeader {
  50.     byte     version,
  51.             lastYY,
  52.             lastMM,
  53.             lastDD;
  54.     dword    lastRec;
  55.     word    headerLen,
  56.             recLen;
  57.     byte    reserved[20];
  58. };
  59.  
  60.  
  61. // Declare the DBOject class
  62. class DBObject {
  63.     private:
  64.         DBHeader        Header;                // Store header of file here
  65.         DBField        *pFields;         // Array of field descriptors
  66.         int         *pFieldOfs;       // Array of field value offsets in buffer
  67.         char             *cFileName;       // Name of the file
  68.         long             nCurPos;          // Current record position starting at 1
  69.         void             *pRecBuff;        // Points to record buffer
  70.         int            nNumFields;       // Number of fields on file
  71.         FILE             *pFilePtr;        // 'C' file stream pointer
  72.         int              nErrorCode;       // Last error code encountered
  73.         bool            bUpdated;         // Set if the file was updated
  74.         bool            bEof;             // Set if at eof
  75.         bool            bBof;             // Set if bof
  76.         bool            bDirty;           // Set if buffer has changed
  77.         bool            bAutoAccess;      // Should record be read/written automatically
  78.  
  79.         // Some private methods - not for client use
  80.         void InitVars(void);
  81.         const long GetFileBytePos(void)        { return(ftell(pFilePtr)); };
  82.         void GoFileBytePos(const long nPos)
  83.             { fseek(pFilePtr, nPos, SEEK_SET); };
  84.         void LoadHeader(void);
  85.         void WriteHeader(void);
  86.         void ClearEof(void)                        { bEof = false; };
  87.         void SetEof(void)                            { bEof = true; };
  88.         void ClearBof(void)                        { bBof = false; };
  89.         void SetBof(void)                            { bBof = true; };
  90.         const bool ValidField(const int n);
  91.  
  92.         // Access members
  93.         void ReadRecord(void);
  94.         void WriteRecord(void);
  95.         void WriteEof(void);
  96.         void ClearBuffer(void);
  97.  
  98.     public:
  99.         // Constructor/destructor members
  100.         DBObject(const char *cFName);
  101.         ~DBObject(void);
  102.  
  103.         // Instantiation members
  104.         void Open(void);
  105.         void Close(void);
  106.  
  107.         // Data access members
  108.         void SetError(int nCode)          { nErrorCode = nCode; };
  109.         void ClearError(void)            { nErrorCode = NoError; };
  110.         void SetDirty(void)                { bDirty = true; };
  111.         void AppendRecord(void);
  112.  
  113.         // Information members
  114.         const char *FileName(void)     { return(cFileName); };
  115.         const int RecLen(void)             { return(Header.recLen); };
  116.         const long LastRec(void)         { return(Header.lastRec); };
  117.         const long RecNo(void)             { return(nCurPos); };
  118.         const int IsError(void)         { return(nErrorCode != NoError); };
  119.         const int ErrorCode(void)         { return(nErrorCode); };
  120.         const bool Exists(void);
  121.         const int IsOpen(void)            { return(pFilePtr != NULL); };
  122.         const int IsClosed(void)        { return(!IsOpen()); };
  123.         const int FieldCount(void)        { return(nNumFields); };
  124.         const bool Eof(void)                { return(bEof); };
  125.         const bool Bof(void)                { return(bBof); };
  126.         const bool IsDeleted(void)        { return( ((char *)pRecBuff)[0] == '*'); };
  127.         void Delete(void)                    { ((char *) pRecBuff)[0] = '*'; SetDirty(); };
  128.         void Recall(void)             { ((char *) pRecBuff)[0] = '*'; SetDirty(); };
  129.  
  130.         const char *FieldName(const int n);
  131.         const char FieldType(const int n);
  132.         const int FieldLen(const int n);
  133.         const int FieldDec(const int n);
  134.         const int FieldOfs(const int n);
  135.         const int FieldPos(const char *name);
  136.  
  137.         // Field access methods
  138.         const char *GetFieldValue(const int n, char *f = NULL);
  139.         void SetFieldValue(const int n, const char *s);
  140.         const char *GetFieldValue(const char *name, char *f = NULL);
  141.         void SetFieldValue(const char *name, const char *s);
  142.         const void *RecBuff(void)            { return(pRecBuff); };
  143.  
  144.         // Positioning members
  145.         void GoRecord(const long nRecNo);
  146.         void GoTop(void);
  147.         void GoBottom(void);
  148.         void GoNextRecord(long n = 1L);
  149.         void GoPrevRecord(long n = 1L);
  150.  
  151.         // Automatic access facilities
  152.         void SetAutoAccessOn(void)        { bAutoAccess = true; };
  153.         void SetAutoAccessOff(void)    { bAutoAccess = false; };
  154.         const bool AutoAccess(void)    { return(bAutoAccess); };
  155. };
  156.  
  157. #endif